using System;

using Slaks.Descriptions;

namespace Slaks.Filters
{
  /// <summary>
  /// ArabTermFilter - implements parsing of arab letters, saving punctuation marks
  /// </summary>
  [ModuleDescriptionAttribute("Arab-English term filter supports saving punctuation marks and special characters","Slava Kisilevich and Alex Markov")]
  public class ArabTermFilterMarkovProject : ArabTermFilter
  {
    protected static readonly int EnglishComma = 0x02c;
    protected static readonly int ArabicComma = 0x060c;

    protected static int EnglishCapitalLetterBegin = 0x41;
    protected static int EnglishCapitalLetterEnd = 0x5a;

    protected static int EnglishSmallLetterBegin = 0x61;
    protected static int EnglishSmallLetterEnd = 0x7a;

    protected static readonly int EndOfSentenceDot = 0x2e;
    protected static readonly int EndOfSentenceEnglishQuestion = 0x3f;
    protected static readonly int EndOfSentenceQuestion = 0x061f;
    protected static readonly int EndOfSentenceExclamation = 0x21;

    protected static readonly int SpecialCharacterLeftSquareBracket = 0x5b; //[
    protected static readonly int SpecialCharacterRightSquareBracket = 0x5d; //]

    public ArabTermFilterMarkovProject() {}

    /// <summary>
    /// </summary>
    /// <param name="ch">character to check</param>
    /// <returns>true/false</returns>
    public override bool IsWhiteSpace(char ch)
    {
      if (ch < EnglishCapitalLetterBegin) return true;
      if (ch > EnglishCapitalLetterEnd && ch < EnglishSmallLetterBegin) return true;
      if (ch > EnglishSmallLetterEnd && ch <= ArabNotInRangeBegin_1) return true;

      if (ch >= ArabNumbersBegin && ch <= ArabNumbersEnd) return true;
      if (ch > ArabNumbersEnd) return true;

      return false;
    }

    /// <summary>
    /// Normalization of the character
    /// Example: Capital letter convert to low
    /// </summary>
    /// <param name="ch">normalized character</param>
    /// <returns>char</returns>
    public override char Normalize(char ch)
    {
      if (ch >= EnglishCapitalLetterBegin && ch <= EnglishCapitalLetterEnd) return char.ToLower(ch);

      if (ch == ArabLastLetterToReplace1 || 
        ch == ArabLastLetterToReplace2 ||
        ch == ArabLastLetterToReplace3) 
        return (char)ArabLastLetterReplaced1_2_3;
      else if (ch == ArabLastLetterToReplace4) 
        return (char)ArabLastLetterReplaced4;
      else if (ch == ArabLastLetterToReplace5) 
        return (char)ArabLastLetterReplaced5;

      else return ch;
    }

    public override bool IsSpecialCharacter(char ch)
    {
      if (ch == SpecialCharacterLeftSquareBracket || ch == SpecialCharacterRightSquareBracket) return true;
      return false;
    }

    public override string ToString()
    {
      return "arab-english filter with saved punctuation for Alex Markov Project";
    }
  }
}
